package cn.snoworld.controller;

import cn.snoworld.dto.CommonResult;
import cn.snoworld.entity.Article;
import cn.snoworld.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("article")
@Api(tags = "文章接口")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("{id}")
    @ApiOperation("根据id获取文章")
    public CommonResult getArticleById(@PathVariable() Integer id) {
        return CommonResult.OK(articleService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增文章")
    public CommonResult addArticle(@RequestBody Article article) {
        articleService.save(article);
        return CommonResult.OK();
    }

    @PutMapping
    @ApiOperation("根据id修改文章")
    public CommonResult modifyArticle(@RequestBody Article article) {
        articleService.updateById(article);
        return CommonResult.OK();
    }

    @DeleteMapping("{id}")
    @ApiOperation("根据id删除文章")
    public CommonResult removeArticleById(@PathVariable Integer id) {
        articleService.removeById(id);
        return CommonResult.OK();
    }
}
